﻿define([], function() {
    var $scope, $q;
    var initVariable = function(scope, q) {
        $scope = scope;
        $q = q;
        $scope.saveSingleDataUrl; //保存单条数据路径
        $scope.getSingleDataUrl; //获取单条数据url
        $scope.formData={}; //表单内容
        $scope.headData={}; //表单所需的表头信息
    }

    var initForm = function(tableItemData, type) {
        if (type == 'edit') {
            getEditFormData(tableItemData);
            return;
        }
        if (type == "add") {
            getAddFormData();
            return;
        }
    }

    function getAddFormData() {
        var promise = myHttpHandler("GET", $scope.getSingleDataUrl + "-1", {});
        var flag = false;
        promise.then(function(datas) {
            $scope.formData = datas.Data;
            $scope.headData = datas.Meta;

            for(var i=0;i<$scope.headData.length;i++){
            	$scope.formData[$scope.headData[i]['column-name']].value="";
            }

            var dialog = $("#_zmAddWindow"+$scope.objId).data('dialog');
            dialog.open();

           adjustForm("_zmAddWindow"+$scope.objId);

        });
    }

    //调整弹出框使居中
    function adjustForm(id) {
        setTimeout(function() {
            var height = $("#"+id).css("height").split("px")[0];
            var screenHeight=$(window).height();

            if (height <= 200) {
            	adjustForm();
            }else{
            	$("#"+id).css("top",(screenHeight-height)/2+"px");
            	return;
            }
        }, 10);
    }

    function getEditFormData(tableItemData) {
        //根据需要上传给服务器的urlDATa获得表单数据
        $scope.tableItemData = tableItemData;

        var rowId = tableItemData.rowId;
        var promise = myHttpHandler("GET", $scope.getSingleDataUrl + rowId, {});
        promise.then(function(datas) {
            $scope.formData = datas.Data;
            $scope.headData = datas.Meta;

            var dialog = $("#_zmEditorWindow"+$scope.objId).data('dialog');
            dialog.open();

            adjustForm("_zmEditorWindow"+$scope.objId);
        });
    }

    var initFunctions = function() {
        $scope.saveZmEditorFormValue = function() {
            // console.log($scope.formData);
            var params = $("#_zmEditForm"+$scope.objId).serialize();
            params = decodeURIComponent(params, true); //对结果进行解码，解决中文乱码问题
            //params like c_1=1&c_2=啊&c_3=2015-03-21&c_4=item1
            var datas = params.split("&");

            //再对updateObject对象进行更新
            for (var i = 0; i < datas.length; i++) {
                var key = datas[i].split("=")[0];
                var value = datas[i].split("=")[1];
                if ($scope.formData[key].type == "number") {
                    $scope.formData[key].value = parseFloat(value);
                    $scope.tableItemData[key] = parseFloat(value); //更新当前界面中表格对应行的信息
                } else {
                    $scope.formData[key].value = value;
                    $scope.tableItemData[key] = value; //更新当前界面中表格对应行的信息
                }
            }

            //上传修改的数据到服务器
            var rowId = $scope.tableItemData.rowId;
            var promise = myHttpHandler("PUT", $scope.saveSingleDataUrl + rowId, { record: angular.toJson($scope.formData) });
            promise.then(function(data) {
                var dialog = $("#_zmEditorWindow"+$scope.objId).data('dialog');
                dialog.close();
            });
        }

        $scope.zmEditorFormClose = function() {
            var dialog = $("#_zmEditorWindow"+$scope.objId).data('dialog');
            dialog.close();
        }

        $scope.saveZmAddFormValue = function() {
            var params = $("#_zmAddForm"+$scope.objId).serialize();
            params = decodeURIComponent(params, true); //对结果进行解码，解决中文乱码问题
            //params like c_1=1&c_2=啊&c_3=2015-03-21&c_4=item1
            var datas = params.split("&");

            //再对updateObject对象进行更新
            for (var i = 0; i < datas.length; i++) {
                var key = datas[i].split("=")[0];
                var value = datas[i].split("=")[1];
                if ($scope.formData[key].type == "number") {
                    $scope.formData[key].value = parseFloat(value);
                } else {
                    $scope.formData[key].value = value;
                }
            }
            //上传修改的数据到服务器
            var promise = myHttpHandler("PUT", $scope.saveSingleDataUrl + "-1", { record: angular.toJson($scope.formData) });
            promise.then(function(data) {
                var dialog = $("#_zmAddWindow"+$scope.objId).data('dialog');
                dialog.close();
            });
        }

        $scope.zmAddFormClose = function() {
            var dialog = $("#_zmAddWindow"+$scope.objId).data('dialog');
            dialog.close();
        }

    }

    function myHttpHandler(type, url, data) {
        var deferred = $q.defer();

        $.ajax({
            async: false,
            type: type,
            url: url,
            data: data,
            dataType: "json",
            error: function(responseData) {
                alert("获取失败!");
            },
            success: function(responseData) {
                deferred.resolve(responseData);
                // console.log("return data="+JSON.stringify(responseData));
            }
        });

        return deferred.promise;
    }

    var defineGetFormDataUrl = function(url) {
        $scope.getSingleDataUrl = url;
    }

    var defineSaveFormDataUrl = function(url) {
        $scope.saveSingleDataUrl = url;
    }

    return {
        initVariable: initVariable,
        initFunctions: initFunctions,
        defineGetFormDataUrl: defineGetFormDataUrl,
        defineSaveFormDataUrl: defineSaveFormDataUrl,
        initForm: initForm,
    }
});
